package com.ruicar.afs.cloud.afscase.channel.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.afscase.channel.common.Constants;
import com.ruicar.afs.cloud.afscase.channel.dto.ChannelAccountInfoDTO;
import com.ruicar.afs.cloud.afscase.channel.dto.ChannelBaseInfoDTO;
import com.ruicar.afs.cloud.afscase.channel.entity.ChannelBaseInfo;
import com.ruicar.afs.cloud.afscase.channel.entity.ChannelReceivablesAccount;
import com.ruicar.afs.cloud.afscase.channel.entity.ChannelRiskInfo;
import com.ruicar.afs.cloud.afscase.channel.service.ChannelAccountInfoService;
import com.ruicar.afs.cloud.afscase.channel.service.ChannelBaseInfoService;
import com.ruicar.afs.cloud.afscase.channel.service.ChannelRiskInfoService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;


/**
 * @ClassName:ArchivedBaseInfoController
 * @Description: 长城归档系统 渠道基本信息外部接口-controller
 * @Author:mingzhi.li
 * @Date:2020/9/11 19:06
 * @Version: V1.0
 **/
@AllArgsConstructor
@RestController
@RequestMapping("/archivCaseInfo")
public class ArchivedBaseInfoController {

    private final ChannelBaseInfoService channelOnlineService;
    private final ChannelAccountInfoService channelAccountInfoService;
    private final ChannelRiskInfoService channelRiskInfoService;
    /**
     * 获取合作商基本信息
     *
     * @param dto
     * @return
     */
    @PostMapping(value = "/getChannelCaseInfo")
    @ApiOperation("获取渠道基本信息")
    public JSONObject getChannelCaseInfo(@RequestBody ChannelBaseInfoDTO dto) {
        JSONObject json = new JSONObject();
        if(StrUtil.isBlank(dto.getDataId())){
            //失败
            json.put("success","0");
            json.put("message","渠道id不能为空");
            json.put("code","9999");
            return json;
        }
        ChannelBaseInfo info = channelOnlineService.getOne(Wrappers.<ChannelBaseInfo>query().lambda()
                .eq(ChannelBaseInfo::getId, dto.getDataId()));
        if(info ==null){
            //失败
            json.put("success","0");
            json.put("message","渠道信息不存在");
            json.put("code","9999");
            return json;
        }
        ChannelReceivablesAccount account = channelAccountInfoService.getOne(Wrappers.<ChannelReceivablesAccount>query().lambda()
                .eq(ChannelReceivablesAccount::getChannelId, dto.getDataId())
                .eq(ChannelReceivablesAccount::getAccountAttribute, Constants.BUSINESS_TYPE_NEW_CAR)
                .eq(ChannelReceivablesAccount::getCollectionType, Constants.COLLECTION_TYPE_ONE)
                .eq(ChannelReceivablesAccount::getStatus, Constants.ACCOUNT_STATUS_ZERO));
        ChannelRiskInfo riskInfoTemp = channelRiskInfoService.getOne(Wrappers.<ChannelRiskInfo>query().lambda().eq(ChannelRiskInfo::getChannelId, info.getId().toString())
                .eq(ChannelRiskInfo::getBusinessType, Constants.BUSINESS_TYPE_NEW_CAR));
        dto.setId(info.getId().toString());
        dto.setName(info.getChannelFullName());
        dto.setCdate(DateUtil.formatDate(info.getCreateTime()));
        dto.setDel( Integer.valueOf(info.getDelFlag()));
        dto.setBusinesslicense(info.getSocUniCrtCode());
        dto.setLegalpersonname(info.getLegalPerson());
        dto.setLegalpersonid(info.getLegalPersonIdCard());
        dto.setRegisteredcapital(info.getRegisteredCapital());
        dto.setResisteredtime(DateUtil.formatDate(info.getRegistrationDate()));
        dto.setLendingofficername(info.getChannelAdmin());
        //管理员地址
        dto.setAddress(info.getChannelAdminAddress());
        //管理员省市
        dto.setCity(info.getChannelAdminCity());
        dto.setMobilephone(info.getChannelAdminTel());
        dto.setEmail(info.getChannelAdminMail());
        if(account !=null){
            dto.setBankname(account.getReceivingBank());
            dto.setOpeningbankname(account.getReceivingSubBranch());
            dto.setBankaccount(account.getAccountType());
            dto.setOpeningbankusername(account.getReceivingName());
            dto.setBankaccount(account.getReceivingAccount());
        }
        if(Constants.VALUE_CHANNEL_STATUS_ZERO.equals(info.getChannelStatus())){
            dto.setStatus(Constants.CC_CHANNEL_STATUS_ZERO);
        }
        if(Constants.VALUE_CHANNEL_STATUS_FOUR.equals(info.getChannelStatus())){
            dto.setStatus(Constants.CC_CHANNEL_STATUS_ONE);
        }

        if(riskInfoTemp !=null){
            dto.setGrade("1");
            dto.setUsercount(riskInfoTemp.getAccountMaxNum());
        }
        dto.setDealernumber(info.getChannelCode());
        dto.setPosition(info.getLongitude()+","+info.getLatitude());
        dto.setCode(Constants.CODE.get(info.getChannelAdminProvince()));
        dto.setName(info.getChannelFullName());
        json.put("success","1");
        json.put("data",dto);
        return json;
    }

    @RequestMapping(value = "/getAccountInfo", method = RequestMethod.POST)
    @ApiOperation(value = "获取渠道收款账号信息")
    public IResponse getAccountInfo(@RequestBody ChannelAccountInfoDTO dto) {
        List<ChannelReceivablesAccount> accountList = channelAccountInfoService.list(Wrappers.<ChannelReceivablesAccount>query().lambda()
                .eq(!ObjectUtils.isEmpty(dto.getReceivingAccount()),ChannelReceivablesAccount::getReceivingAccount, dto.getReceivingAccount()));
        List<ChannelAccountInfoDTO> dtoList = new ArrayList<>();
        if(accountList!=null && accountList.size()>0){
            for(ChannelReceivablesAccount temp:accountList){
                ChannelAccountInfoDTO infoDTO = new ChannelAccountInfoDTO();
                infoDTO.setReceivingAccount(temp.getReceivingAccount());
                infoDTO.setReceivingSubBranch(temp.getReceivingSubBranch());
                dtoList.add(infoDTO);
            }
        }
        return IResponse.success(dtoList);
    }
}
